package com.example.financechain.Utils;


import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
//Captcha 生成工具
public class CaptchaUtil {
    @Autowired
    private DefaultKaptcha producer;

    //生成catchCreator的map
    public Map<String, Object> catchaImgCreator() throws IOException {
        //生成文字验证码
        String text = producer.createText();
        //生成文字对应的图片验证码
        BufferedImage image = producer.createImage(text);
        //将图片写出
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", outputStream);
        //对写出的字节数组进行Base64编码 ==> 用于传递8比特字节码

        //生成token
        //Map<String, Object> token = captchaService.createToken(text);
//        token.put("imgcaptcha", encoder.encode(outputStream.toByteArray()));
//        return token;
        Map<String,Object> Info = new HashMap<>();
        Info.put("text",text);
        Info.put("encoder", Base64.encodeBase64String(outputStream.toByteArray()));

        return Info;
    }
}
